import json

def translate_tree(the_id, target_file_name):
    f = open("sourcedata/" + the_id + "_matrixID.json")
    myjson = json.load(f)
    f.close()
    relation = dict()
    for key in list(myjson):
        mat_id = myjson[key]
        if not relation.has_key(mat_id):
            relation[mat_id] = list()
        relation[mat_id].append(key)
    f = open(target_file_name)
    myjson = json.load(f)
    f.close()
    for small_cluster in myjson[7]:
        tmp_clus = list()
        for mat_id in small_cluster['clus']:
            real_ids = relation[mat_id]
            for real_id in real_ids:
                tmp_clus.append(real_id)
        small_cluster['clus'] = tmp_clus
    f = open("json/" + the_id + "_tree.json", 'w')
    str2write = json.dumps(myjson)
    f.write(str2write)
    f.close()

